<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>系统字典管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../statics/layui/css/layui.css">
    <link rel="stylesheet" href="../../../statics/layui/font/font.css" media="all"/>

</head>
<body>
<div class="content">
    <form class="layui-form" style="margin-top: 10px;">
         <div class="layui-inline">
            <label class="layui-form-label">字典名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">分组编码：</label>
            <div class="layui-input-inline">
                <input type="text" name="code" id="code" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-input-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="formDemo">搜索</button>
            <button class="layui-btn layui-btn-sm" >清空</button>
        </div>
    </form>
    <table id="dataList" lay-even lay-filter="dataList"></table>
    <script type="text/html" id="toolbarOrg">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
            <button class="layui-btn layui-btn-sm" lay-event="del">批量删除</button>
        </div>
    </script>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="change">修改</a>
    </script>
</div>
<script type="text/javascript" src="../../../statics/layui/layui.js"></script>
<script src="/static/js/main.js"></script>

<script>
    layui.use(['form', 'sessionFun', 'configure', 'configureCom', 'table'], function () {
        var $ = layui.$;
        var form = layui.form;
        var configure = layui.configure;
        var table = layui.table;
        var configureCom = layui.configureCom;
        var sessionFun = layui.sessionFun;

        var cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'name', title: '分组名称'},
            {field: 'code', title: '分组编码'},
            {field: 'createTime', title: '分组时间'},
            {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
        ]];

        configureCom.tableRender(configure.DICT_GROUP_LIST_HTTP, {}, cols, 'dataList', 10, 'full-100', 'toolbarOrg');
        configureCom.tableSearch();

        //头工具栏事件
        table.on('toolbar(dataList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            sessionFun.remove(configure.SESSION_LIST_DATA);
            switch (obj.event) {
                case 'add':
                    editItem('新增字典组', configure.DICT_GROUP_SAVE_HTTP);
                    break;
                case 'del':
                    var data = checkStatus.data;
                    if (!data.length) {
                        top.layer.alert('请您至少选择一条数据后再提交！');
                        return false;
                    }
                    top.layer.confirm('您真的要删除选中的数据吗？', function (index, layero) {
                        top.layer.close(index);
                        var ids = '';
                        $(data).each(function (index, item) {
                            if(!index){
                                ids = item.id;
                            }else{
                                ids += ','+item.id;
                            }
                        });
                        deleteFun(ids);
                    });
                    break;
            }
        });
        //每行操作
        table.on('tool(dataList)', function (obj) {
            var data = obj.data;
            sessionFun.set(configure.SESSION_LIST_DATA, data);
            if (obj.event === 'del') {//查看详情
                deleteFun(data.id);
            } else if (obj.event === 'change') {
                editItem('修改字典组', configure.DICT_GROUP_UPDATE_HTTP)
            }
        });
        function deleteFun(ids){
            configureCom.postDataAjax(configure.DICT_GROUP_DELETE_HTTP, {ids:ids}).then(function (res) {
                if(res.code==0){
                    top.layer.msg('删除成功！');
                    configureCom.tableReload(true);
                }else{
                    top.layer.alert('删除失败！');
                }
            });
        }
        //新增或修改字典详情
        function editItem(title, url) {
            top.layer.open({
                type: 2,
                title: title,
                area: configureCom.Psize1,
                content: configureCom.nocache(configure.DICT_GROUP_DETAIL_HTML),
                btn: ['保存'],
                yes: function (index, layero) {
                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对
                    var data = iframeWin.getData();
                    configureCom.postDataAjax(url, data).then(function (res) {
                        if (res.code == 0) {
                            top.layer.close(index);
                            top.layer.msg('保存成功！');
                            configureCom.tableReload(true);
                        } else {
                            top.layer.alert('保存失败！');
                        }
                    });
                }
            });
        }
    });
</script>
</body>
</html>
